//
//  ZYCCompleteBasicInformationCell.m
//  ZDSC
//
//  Created by 张威 on 2021/10/15.
//

#import "ZYCCompleteBasicInformationCell.h"
#define MAX_STARWORDS_LENGTH 18
@interface ZYCCompleteBasicInformationCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textField;


@end
@implementation ZYCCompleteBasicInformationCell

- (void)setTitleString:(NSString *)titleString{
    _titleString = titleString;
    if ([titleString containsString:@"*"]) {
        _titleLabel.attributedText = [self returnStringWithString:titleString];
    }else{
        _titleLabel.text = titleString;
    }
}

- (void)setPlaceHolderString:(NSString *)placeHolderString{
    _placeHolderString = placeHolderString;
    _textField.placeholder = placeHolderString;
    _textField.textColor = HEX_RGB(@"#999999");
}

- (void)setContentString:(NSString *)contentString{
    _contentString = contentString;
    if (contentString.length != 0) {
        _textField.text = contentString;
        _textField.textColor = HEX_RGB(@"#333333");
    }else{
        _textField.textColor = HEX_RGB(@"#999999");
    }
}

- (NSMutableAttributedString *)returnStringWithString:(NSString *)string{
    NSMutableAttributedString * attriString = [[NSMutableAttributedString alloc] initWithString:string];
    [attriString addAttribute:NSForegroundColorAttributeName value:HEX_RGB(kTitleRedColor) range:NSMakeRange(0, 1)];
    return attriString;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    if ([self.titleString isEqualToString:@"*所在年级:"] || [self.titleString isEqualToString:@"*所在班级:"] || [self.titleString isEqualToString:@"*性别:"]) {
        _textField.enabled = NO;
        
    }else if([self.titleString isEqualToString:@"*身份证号:"]){
        _textField.keyboardType = UIKeyboardTypeNamePhonePad;
    }else{
        _textField.enabled = YES;
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.textField];

    
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.width.mas_equalTo(100.f);
            make.centerY.equalTo(self.contentView);
        }];
        
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(self.titleLabel.mas_right);
            make.right.equalTo(self.contentView);
            make.centerY.equalTo(self.contentView);
        }];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:)
                  name:UITextFieldTextDidChangeNotification object:self.textField];

        
    }
    return self;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Regular", 16.0f) titleColor:HEX_RGB(@"#333333") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.delegate = self;
        _textField.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        _textField.textColor = HEX_RGB(@"#999999");
        _textField.textAlignment = NSTextAlignmentLeft;
    }
    return _textField;
}



#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)noti{
    UITextField *textField = (UITextField *)noti.object;
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position){
        if (toBeString.length > MAX_STARWORDS_LENGTH)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    if (_textField.text.length == 0) {
        _textField.textColor = HEX_RGB(@"#999999");
    }else{
        _textField.textColor = HEX_RGB(@"#333333");
    }

    if (self.textChangedBlock) {
        self.textChangedBlock(self.titleString,textField.text);
    }
}



@end
